//
//  ParHomeworkTableViewCell.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/10/9.
//  Copyright © 2016年 syun. All rights reserved.
//
#define TEACHERCELLID @"mycellid"  //我发布的
#define CLASSCELLID @"cellid"  //班级相关的

#import "ParHomeworkTableViewCell.h"
#import "SDPhotoBrowser.h"

@interface ParHomeworkTableViewCell()<SDPhotoBrowserDelegate>

@end
@implementation ParHomeworkTableViewCell{
    
    int count; //照片的数量
    
    NSArray *_imageArr;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        /*
         subLabel; //科目
         timeLabel; //事件
         contentLabel; //内容
         sourceLabel;  //来源
         icView *picImageView;
         */
        
        UIView *superView = self.contentView;
        
        _selectBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_nor_"] forState:UIControlStateNormal];
        [_selectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 12)];
        [superView addSubview:_selectBtn];

        
        //科目
        _subLabel = [[UILabel alloc] init];
        _subLabel.font = [UIFont systemFontOfSize:17];
        [superView addSubview:_subLabel];
        
        //时间
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = RGBACOLOR(151, 151, 151, 1);
        _timeLabel.textAlignment = NSTextAlignmentRight;
        
        [superView addSubview:_timeLabel];
        
        
        //完成章
        _finshImage=[[UIImageView alloc]init];
        _finshImage.image=[UIImage imageNamed:@"finshWork"];
        
        [superView addSubview:_finshImage];
        
        
        //内容
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:15];
        _contentLabel.textColor = RGBACOLOR(155, 155, 155, 1);
        _contentLabel.numberOfLines = 2;
        [superView addSubview:_contentLabel];
        
        //来源
        _sourceLabel = [[UILabel alloc] init];
        _sourceLabel.font = [UIFont systemFontOfSize:12];
        _sourceLabel.textColor = RGBACOLOR(59, 59, 59, 1);
        [superView addSubview:_sourceLabel];
        
        //图片
        _picImageView = [[homeworkPicView alloc] init];
        _picImageView.userInteractionEnabled = YES;
        _picImageView.taskImageView.userInteractionEnabled = YES;
        
        //是否已读
        _isreadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _isreadBtn.hidden = YES;
        [_isreadBtn setImage:[UIImage imageNamed:@"little_redcircle_"] forState:UIControlStateNormal];
        [superView addSubview:_isreadBtn];
        
        
        [superView addSubview:_picImageView];
        
        
        //完成章
        _finshImage=[[UIImageView alloc]init];
        _finshImage.image=[UIImage imageNamed:@"finshWork"];
        [superView addSubview:_finshImage];
        
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(superView.mas_left).offset(12);
            make.top.equalTo(superView.mas_top).offset(39);
            make.height.equalTo(@20);
            //            make.right.equalTo(self->_timeLabel.mas_left).offset(-15);
            make.width.equalTo(@32);
            
        }];
        
        //已读按钮
        [_isreadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self->_selectBtn.mas_right).offset(0);
            make.top.equalTo(superView.mas_top).offset(20.5);
            make.height.equalTo(@7);
            //            make.right.equalTo(self->_timeLabel.mas_left).offset(-15);
            make.width.equalTo(@16);
            
        }];
        
        [_subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self->_isreadBtn.mas_right).offset(0);
            make.top.equalTo(superView.mas_top).offset(15);
            make.height.equalTo(@18);
            make.right.equalTo(self->_timeLabel.mas_left).offset(-5);
            //            make.width.equalTo(@100);
            
        }];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(superView.mas_top).offset(19);
            make.right.equalTo(self->_picImageView.mas_left).offset(-15);
            make.left.equalTo(self->_subLabel.mas_right).offset(5);
            
            make.height.equalTo(@13);
            //            make.width.equalTo(@100);
        }];
        
        [_finshImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //make.top.equalTo(superView.mas_bottom).offset(18);
            make.top.equalTo(superView.mas_top).offset(18);
            make.right.equalTo(superView.mas_right).offset(-70);
            make.height.equalTo(@65);
            make.width.equalTo(@65);
        }];
        

        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self->_subLabel.mas_bottom).offset(10);
            make.right.equalTo(self->_picImageView.mas_left).offset(-15);
            make.left.equalTo(self->_selectBtn.mas_right).offset(0);
            make.height.equalTo(@15);
        }];
        
        
        [_picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(superView.mas_top).offset(17);
            make.right.equalTo(superView.mas_right).offset(-12);
            make.height.equalTo(@64);
            make.width.equalTo(@64);
        }];
        
        
        [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self->_selectBtn.mas_right).offset(0);
            make.top.equalTo(self->_contentLabel.mas_bottom).offset(11);
            make.bottom.equalTo(superView.mas_bottom).offset(-17);
            make.right.equalTo(self->_picImageView.mas_left).offset(-15);
        }];
        
    }
    
    return self;
}

-(void)updateCellWithModel:(homework *)model{
    
//    if ([model.coursename isEqualToString:@"<null>"]) {
//        model.coursename
//    }
    
    _subLabel.text = [NSString stringWithFormat:@"%@作业",model.coursename.length > 0 ? model.coursename : @""];
    
    
    
//    //已读按钮
//    if (model.isread == nil) {  //这种情况是学生后加入的班级
//        
//        _isreadBtn.hidden = YES;
//        
//    }else{
//        
        if ([model.isread integerValue] == 0) {
            
            _isreadBtn.hidden = NO;
            [_isreadBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@16);
            }];
            
            [_subLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(self->_isreadBtn.mas_right).offset(0);
                
            }];

            
        }else if([model.isread integerValue] == 1){
            
            _isreadBtn.hidden = YES;
            [_isreadBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@0);
            }];
            [_subLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(self->_isreadBtn.mas_right).offset(0);
                
            }];

        }
//    }
    
    
    NSString *timeStr = model.jointime;
    
    timeStr = [Utils getDataStringWithoutSecondString:model.jointime];
    

    _timeLabel.text = timeStr;
    
    if (model.scontent.length > 0) {
        
        _contentLabel.text = model.scontent;
    }else{
        _contentLabel.text = @"作业内容,请点击进入查看图片信息";
    }
    
    //已读人数
    
    NSString *str = [NSString stringWithFormat:@"已读:  %d/%d",(int)model.readnum,(int)model.allnum];
    
    NSRange range = [str rangeOfString:@"/"];
    CGFloat len = range.location - 4;
    NSRange newRange = NSMakeRange(4, len);
    
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: RGBACOLOR(56, 173, 255, 1)} range:newRange];
    
    _readCountLabel.attributedText = attriStr;
    
    
    //来源
    if (model.ismyPub == 1) {
        _sourceLabel.text = [NSString stringWithFormat:@"发送对象:  %@",model.tousername];
    }else{
        _sourceLabel.text = [NSString stringWithFormat:@"%@   %@",model.classname,model.username];
    }
    
    NSString *fieldStr = model.fileid;
    
    NSArray *images;
    
    if (fieldStr.length > 0) { //有图片的情况
        
        _picImageView.hidden = NO;
        
        images = [fieldStr componentsSeparatedByString:@","];
        _imageArr = images;
        count = (int)images.count;
        NSString *countStr = [NSString stringWithFormat:@"%lu张",(unsigned long)images.count];
        NSString *logoStr = images[0];
        
         if (![logoStr hasPrefix:@"http"] && logoStr) {
            logoStr = [PICPERFIXURL stringByAppendingString:logoStr];
            logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
        }
        
        if (images.count == 1) {
            
            _picImageView.numberBtn.hidden = YES;
        }else if(images.count > 1){
            _picImageView.numberBtn.hidden = NO;
            [_picImageView.numberBtn setTitle:countStr forState:UIControlStateNormal];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_picImageView addGestureRecognizer:tap];
        
        
        [_picImageView.taskImageView sd_setImageWithURL:[NSURL URLWithString:logoStr] placeholderImage:[UIImage imageNamed:@"emptyPic"]];
    }else{ //没有图片的情况
        
        _picImageView.taskImageView.image = [UIImage imageNamed:@"nopic_back"];
        
        for (UIGestureRecognizer *tap in _picImageView.gestureRecognizers) {
            [tap removeTarget:self action:@selector(tapAction:)];
        }
        
        _picImageView.numberBtn.hidden = YES;
    }
    
    
    if (model.isfinish==1) {
        
        _finshImage.hidden=NO;
        
    }else
    {
        _finshImage.hidden=YES;
    }
    
}

-(void)tapAction:(UIGestureRecognizer *)tap{
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = _picImageView;
    browser.imageCount = count;
    
    browser.currentImageIndex = 0;
    browser.delegate = self;
    [browser show];
}

//-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
//
//    NSString *str = _imageArr[index];
//
//    NSString *logoStr = [PICPERFIXURL stringByAppendingString:str];
//
//    UIImageView *imageView = [[UIImageView alloc] init];
//
//    __block UIImage *_image;
//    [imageView sd_setImageWithURL:[NSURL URLWithString:logoStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//
//        _image = image;
//    }];
//    return _image;
//}
//
//- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{
//
//    NSString *str = _imageArr[index];
//
//    NSString *logoStr = [PICPERFIXURL stringByAppendingString:str];
//
//    NSURL *url = [NSURL URLWithString:logoStr];
//    return url;
//}
-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _imageArr[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    __block UIImage *_image;
    [imageView sd_setImageWithURL:[NSURL URLWithString:logoStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        _image = image;
    }];
    return _image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _imageArr[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    
    NSURL *url = [NSURL URLWithString:logoStr];
    return url;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
